//
//  QueRenDingDanViewController.m
//  EJLX
//
//  Created by adc on 15/11/26.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "QueRenDingDanViewController.h"
#import "FirstQueRenDingDanTableViewCell.h"
#import "QueRenZhiFuViewController.h"
#import "SecondQueRenDingDanTableViewCell.h"
#import "ThireQueRenDingDanTableViewCell.h"
#import "HttpsManager.h"
#import "GoodsAdressVC.h"
#import "shouYinTaiViewController.h"
#import "ShangPinModel.h"
#import "QueRenDingDan.h"
@interface QueRenDingDanViewController ()

@end

@implementation QueRenDingDanViewController
-(void)viewWillAppear:(BOOL)animated
{
    //
    
    [_tabelView reloadData];
    NSLog(@"zzzzz%lu",(unsigned long)_shangPinAry.count);
    NSLog(@"%@---%@--%@",self.people.name,self.people.phone,self.people.address);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"qqq%@",_diZhiName);
    self.view.backgroundColor=RGB(240, 240, 240);

    if(!_people){
        _people=[[People alloc]init];
    }
       UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    for (int i=0; i<_shangPinAry.count; i++)
    {
        ShangPinModel*sP=_shangPinAry[i];
        int a=[sP.jiaGe intValue];
        int b=[sP.shuLiang intValue];
        int c=a*b;
        int z=c+z;
        _heJi.text=[NSString stringWithFormat:@"%d",z];
    }
    
   // int c=a*b;
    
    [self.view addSubview:view];
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10,7,30,30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-55, 10,110, 24)];
    label.text=@"确认订单";
    label.textColor=[UIColor whiteColor];
    label.textAlignment=NSTextAlignmentCenter;
    [view addSubview:label];
    [_tabelView registerNib:[UINib nibWithNibName:@"FirstQueRenDingDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
     [_tabelView registerNib:[UINib nibWithNibName:@"SecondQueRenDingDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
     [_tabelView registerNib:[UINib nibWithNibName:@"ThireQueRenDingDanTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    _tabelView.tableFooterView=[[UIView alloc]init];
    
    
    
    
    // Do any additional setup after loading the view from its nib.
}
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 2;
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        
        GoodsAdressVC*vc=[[GoodsAdressVC alloc]init];
        vc.mainVc=self;
        vc.people=self.people;
        [self.navigationController pushViewController:vc animated:YES];
        
        
        
        
        
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section==0)
    {
        return 1;
    }
    if (section==1)
    {
        return _shangPinAry.count;
    }
    
        return 1;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        return 80;
    }
    if (indexPath.section==1)
    {
        return 100;
    }
    
    return 44;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 5;
//}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)
    {
        FirstQueRenDingDanTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
        cell.shouHuoRen.text=self.people.name;
        cell.phone.text=self.people.phone;
        cell.shouHuoDiZhi.text=self.people.address;
        return cell;
        
    }
    if (indexPath.section==1)
    {
        ShangPinModel*shangP=_shangPinAry[indexPath.row];
        SecondQueRenDingDanTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
       
       
        cell.name.text=shangP.name;
        cell.jiaGe.text=shangP.jiaGe;
        cell.shuLiang.text=shangP.shuLiang;
        [cell.tuPian sd_setImageWithURL:[NSURL URLWithString:shangP.image] placeholderImage:[UIImage imageNamed:@"头像"]];
        cell.yanSe.text=shangP.yanSe;
        cell.size.text=shangP.chiMa;
        return cell;
    }
    ThireQueRenDingDanTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
    return cell;
    
}
-(void)backBut
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)queRen:(UIButton *)sender
{

    if (_a==1)
    {
        ShangPinModel*shangP=_shangPinAry[0];
        JiaRuModel*jiaRu=[[JiaRuModel alloc]init];
        jiaRu.productId=shangP.productId;
        jiaRu.shopId=shangP.shopId;
        jiaRu.attr1Id=shangP.attr1Id;
        jiaRu.attr2Id=shangP.attr2Id;
        jiaRu.num=shangP.shuLiang;
        jiaRu.addresseeId=self.people.addressId;
      
        NSMutableString*order=[[NSMutableString alloc]init];
     
        [HttpsManager requestLiJiXiaDan:jiaRu completion:^(NSString *orderId, NSString *money, NSString *errmsg, int errcode)
         {
             NSLog(@"err %@ code %d",errmsg,errcode);

             if (errcode==0)
             {
                 shouYinTaiViewController*vc=[[shouYinTaiViewController alloc]init];
                 vc.mony=money;
                 [order appendString:orderId];
                 vc.orderId=order;
                 vc.o2o=@"false";
                 [self.navigationController pushViewController:vc animated:YES];
             }
             else
             {
                 SHOW_ALERT(errmsg);
             }
         }];

    }
    
    else
    {
        NSMutableArray*itemsAry=[NSMutableArray array];
        
        for (int i=0;i<_shangPinAry.count; i++)
        {
            
            ShangPinModel*shang=_shangPinAry[i];
           
            [itemsAry addObject:shang.Id];
      
        }
        NSString*str=self.people.addressId;
      
        
        [HttpsManager requestqueRenXiaDan:itemsAry address:str completion:^(NSMutableArray *ordersAry, int totalMoney, NSString *errmsg, int errcode)
         
        {
            
            NSLog(@"err %@ code %d",errmsg,errcode);
             NSMutableString*order=[[NSMutableString alloc]init];
            
            if (errcode==0)
            {
                shouYinTaiViewController*vc=[[shouYinTaiViewController alloc]init];
                vc.mony=[NSString stringWithFormat:@"%.2f",totalMoney*0.01];
                for (int i=0; i<ordersAry.count; i++)
                {
                    QueRenDingDan*queRen=ordersAry[i];
                    [order appendString:[NSString stringWithFormat:@"%d",queRen.orderId]];
                }
                
                vc.orderId=order;
                vc.o2o=@"false";
                [self.navigationController pushViewController:vc animated:YES];
            }
            else
            {
                SHOW_ALERT(errmsg);
            }
        }];

        
            
            
            
            
            
            
       
        
    }
    
    
    
    
    
    
    
    
    
}


@end
